package com.hantanger.weblog.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hantanger.weblog.model.Post;
import com.hantanger.weblog.util.HtmlUtil;
import com.hantanger.weblog.util.UserHelper;

public class PostListController extends BaseController {

	private static final String TYPE_LIST = "list";
	
	private static final String TYPE_TAG = "tag";
	
	private static final String TYPE_ARCHIVE = "archive";
	
	@Override
	public String doGet(HttpServletRequest request, HttpServletResponse response) {
		String userAgent = request.getHeader("User-Agent");
		if(userAgent.contains("Writer")){
			Map<String,Object> data = new HashMap<String,Object>();
			data.put("blogLink", "http://www.hantanger.com");
			response.setContentType(HTML);
			String discover =  getStringMerger().mergeTemplate(data, "metaweblog/discover.ftl");
			try {
				PrintWriter writer = response.getWriter();
				writer.write(discover);
				writer.flush();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}
		
		int limit = 10;
		//-1表式所有日志, -2草稿
		boolean isPostMore = false;
		long publishTime = HtmlUtil.getLong(request, "p", 0);
		if(publishTime > 0){
			isPostMore = true;
		}else{
			publishTime = System.currentTimeMillis();
		}
		String type = HtmlUtil.getString(request, "type", "list");
		List<Post> posts = null;
		if(TYPE_LIST.equals(type)){
			posts = doList(request, limit, publishTime);
		}else if(TYPE_TAG.equals(type)){
			posts = doTag(request, limit, publishTime);
		}else if(TYPE_ARCHIVE.equals(type)){
			posts = doArchive(request, limit, publishTime);
		}
		if(posts != null && posts.size() == limit){
			request.setAttribute("hasMorePost", true);
			posts = posts.subList(0, limit - 1);
			request.setAttribute("lastPublishTime", posts.get(limit-2).getPublishTime());
		}
		request.setAttribute("posts", posts);
		request.setAttribute("isPostMore", isPostMore);
		if(isPostMore){
			render("/postlist.ftl", request, response);
			return null;
		}
		request.setAttribute("isPostListPage", true);
		return "postlist";
	}
	
	private List<Post> doList(HttpServletRequest request, int limit, long publishTime){
		long categoryId = HtmlUtil.getLong(request, "categoryId", -1);
		return getPostService().getPostsByCategory(categoryId, limit, publishTime, !UserHelper.isAdmin());
	}
	
	private List<Post> doTag(HttpServletRequest request, int limit, long publishTime){
		String tagName = HtmlUtil.getString(request, "tagName", "");
		return getPostService().getPostsByTagName(tagName, limit, publishTime, !UserHelper.isAdmin());
	}
	
	private List<Post> doArchive(HttpServletRequest request, int limit, long publishTime){
		long archiveId = HtmlUtil.getLong(request, "archiveId", -1);
		return getPostService().getPostsByArchive(archiveId, limit, publishTime, !UserHelper.isAdmin());
	}
	
	@SuppressWarnings("unchecked")
	@Override
	protected void setSEO(HttpServletRequest request){
		List<Post> posts = (List<Post>)request.getAttribute("posts");
		if(posts != null){
			StringBuilder sb = new StringBuilder(300);
			for(Post post : posts){
				sb.append(post.getTitle()).append(",");
			}
			request.setAttribute("description", sb);
		}
		
	}

}
